﻿#NoEnv
#Warn
#SingleInstance force
SetWorkingDir %A_ScriptDir%
FileEncoding , UTF-8-RAW
CoordMode, tooltip, Screen
CoordMode, Caret, Screen

Menu, Tray, Icon, %A_ScriptDir%\图片\搜索.ico,1,1  ;改变任务栏图标

menu, 编辑, add, 设为默认`t(&D), 设为默认
menu, 编辑, add, 编辑`t(&E), 编辑
menu, 编辑, add, 删除`tDelete, 删除

IniRead, 默认, %A_ScriptDir%\数据\settings.ini, 默认方式, 默认, https://www.baidu.com/#ie=UTF-8&wd=搜索词
IniRead, 默认名称, %A_ScriptDir%\数据\settings.ini, 默认方式, 默认名称, 百度

lj:=[]
lj_Name:=[]
key:=[]
Loop, Read, %A_ScriptDir%\数据\Date
{
	if not A_LoopReadLine
		continue
	b:=StrSplit(StrReplace(A_LoopReadLine, "`r`n"), ";")
	key.Push(b[1])
	lj_name.Push(b[2])
	lj.Push(b[3])
}

gui, -Caption -Border +Hwndid
gui, font, s18 
Gui, Color , , f2eada
Gui Add, Picture, x0 y0 w1080 h150 , %A_ScriptDir%\图片\背景.png

if A_Args.Length() >= 1
{
Gui Add, Edit, g实时搜索 vtype x100 y95 w50 h30 -Multi -WantReturn Center -VScroll -HScroll, % A_Args[2]
Gui Add, Edit,  vq xp+50 yp w830 h30 -Multi -WantReturn Center -VScroll -HScroll, % A_Args[1]
gosub 搜索

}
else
{
Gui Add, Edit, g实时搜索 vtype x100 y95 w50 h30 -Multi -WantReturn Center -VScroll -HScroll
Gui Add, Edit,  vq xp+50 yp w830 h30 -Multi -WantReturn Center -VScroll -HScroll
}

Gui Add, Picture, g搜索 x508 y30 AltSubmit BackgroundTrans, %A_ScriptDir%\图片\搜索.png
Gui Add, Picture, g帮助 xp+150 yp+20 AltSubmit BackgroundTrans, %A_ScriptDir%\图片\帮助.png
gui add, button , g搜索 Hidden Default  ;隐藏该按钮（回车可以激活该按钮）
Gui Show, w1080 h150, Search

gui, help:Default
Gui, help:+Hwndid
GroupAdd, helpzu , ahk_id %id%
Gui, help: Color, FFFFFF, FFFFFF
gui, help: font, s13, SimHei
gui, help: add, Text, x230 y10 , 设置

gui, help: add, Text, vmoren_tip x200 yp+20 , 默认方式: %默认名称%

gui, help: add, ListView, x5 yp+40 w500 r10 -Multi NoSortHdr +ReadOnly , 关键字|名称|链接
Loop % lj_name.Length()
LV_Add("-focus" , key[A_Index], lj_name[A_Index], lj[A_Index])
Loop 4
LV_ModifyCol(A_Index, "AutoHdr")

gui, help: add, Picture, x240 yp+250 g保存记录 , %A_ScriptDir%\图片\Check.png

gui, help: add, Text, x5 yp+40 , 关键字:
gui, help: add, Edit, xp+70 yp w400 vthe_key WantReturn

gui, help: add, Text, x5 yp+40 , 名称:
gui, help: add, Edit, xp+70 yp w400 vthe_name WantReturn

gui, help: add, Text, x5 yp+40 , 链接:
gui, help: add, Edit, xp+70 yp w400 vthe_lj WantReturn

gui, help: add, button, Hidden Default g保存记录, Ok
gui, %id%:Default

Return

保存记录:
gui, help: Default
gui, help: Submit, NoHide
for a, b in [the_key, the_name , the_lj]
{
	if not b
	{
		Tip("请补全")
		return
	}
}

key.Push(StrReplace(the_key, "`r`n"))
lj_name.push(StrReplace(the_name, "`r`n"))
lj.Push(StrReplace(the_lj, "`r`n"))

file := FileOpen(A_ScriptDir "\数据\Date", "w")
		for a,b in key
		{
			file.Write(b ";" lj_name[a] ";" lj[a] "`r`n")
		}
		file.Close()

LV_Add("select" , the_key, the_name, the_lj)
LV_Modify(key.Length(), "Vis")
Tip("已保存")

for a, b in ["the_key", "the_name" , "the_lj"]
GuiControl, help:, %b%, 

gui, %id%: Default
return

设为默认:
gui, help: Default
if rownumber:=LV_GetNext(0)
{
	LV_GetText(默认, rownumber, 3)
	LV_GetText(默认名称, rownumber, 2)
	IniWrite, %默认%, %A_ScriptDir%\数据\settings.ini, 默认方式, 默认
	IniWrite, %默认名称%, %A_ScriptDir%\数据\settings.ini, 默认方式, 默认名称
	Tip(默认名称 " 已设为默认方式")
	GuiControl, help:, moren_tip, 默认方式: %默认名称%
}
else
	Tip("请选择要设为默认的一项")
gui, %id%: Default
return

编辑:
gui, help: Default
if rownumber:=LV_GetNext(0)
{
	for a, b in ["the_key", "the_name" , "the_lj"]
	{
		LV_GetText(c, rownumber, a)
		GuiControl, help:, %b%, %c%
	}
	LV_Delete(rownumber)
	
key.RemoveAt(rownumber)
lj_name.RemoveAt(rownumber)
lj.RemoveAt(rownumber)

file := FileOpen(A_ScriptDir "\数据\Date", "w")
		for a,b in key
		{
			file.Write(b ";" lj_name[a] ";" lj[a] "`r`n")
		}
		file.Close()
}
else
	Tip("请选择要编辑的一项")
gui, %id%: Default
return

#IfWinActive, ahk_group helpzu
Delete::
删除:
gui, help: Default
if rownumber:=LV_GetNext(0)
{
	LV_Delete(rownumber)
	
	key.RemoveAt(rownumber)
	lj_name.RemoveAt(rownumber)
	lj.RemoveAt(rownumber)

file := FileOpen(A_ScriptDir "\数据\Date", "w")
		for a,b in key
		{
			file.Write(b ";" lj_name[a] ";" lj[a] "`r`n")
		}
		file.Close()
}
else
	Tip("请选择要 删除 的一项")
gui, %id%: Default
return
#IfWinActive

helpGuiContextMenu:
Menu, 编辑, show
return

GuiEscape:
GuiClose:
    ExitApp
 
帮助:
MsgBox 0x40040, , 在两个搜索栏分别填入 "关键词"   "搜索内容"`n`n 如  bd     你不对劲`n`n若忽略“关键词”默认使用百度搜索“搜索内容”
gui, help:Show, w540
return


实时搜索:
gui submit, nohide
for k,kk in key
	{
		if (Type = kk)
		{
			tip(lj_name[k], 2000 ,A_CaretX, A_CaretY+50)
			break
		}
	}
return

搜索:
gui submit, nohide
q:=Trim(q, A_Space A_Tab)
NeedleRegEx =(https?|ftp|file)://[-A-Za-z0-9+&@#/`%?=~_|!:,.;]+[-A-Za-z0-9+&@#/`%=~_|]
if RegExMatch(q, NeedleRegEx) = 1
{
	run %q%
	exitapp
}


if (q or type)
{
	for k,kk in key
	{
		if (Type = kk)
		{
			Type_num:=k
			break
		}
	}
if (k=key.Length())
	Type_num:=0


if Type_num
run % StrReplace(lj[Type_num], "搜索词", q)
else
	run % StrReplace(默认, "搜索词", q)
q:=
}
exitapp



Tip(Text:="", time:=1000 ,x:=-100, y:=-100){
	Critical, On
	if (x=-100 and y=-100)
		ToolTip, %Text%
	else
		ToolTip, %Text%, %x%, %y%
	SetTimer, f_RemoveToolTip, -%time%
	return

f_RemoveToolTip:
	ToolTip
return
}

!q::
send ^s
Reload